﻿using System;
using System.Collections.Generic;

using System.Linq;
using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 基本内容的方法集合
    /// </summary>
    public partial class CoreDal
    {
        /// <summary>
        /// 读取被选择的Id范围的内容,返回Id,CategoryId,TemplateFileName
        /// </summary>
        /// <returns></returns>
        public IList<Core> ReadContent(byte modelId)
        {
            using (Entities db = new Entities())
            {
                var query =
                    db.Cores.AsNoTracking().Where(x => !x.IsDeleted && x.IsChecked && x.Category.ModelId == modelId)
                      .OrderByDescending(x => x.Id)
                      .Select(x => new { x.Id, x.CategoryId, x.TemplateFileName, x.UpdateTime });

                return query.ToArray().Select(x =>
                    new Core() { Id = x.Id, CategoryId = x.CategoryId, TemplateFileName = x.TemplateFileName, UpdateTime = x.UpdateTime }).ToArray();
            }
        }

        /// <summary>
        /// 读取前N条内容,返回Id,CategoryId,TemplateFileName
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public IList<Core> ReadContent(byte modelId, int top)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Cores.AsNoTracking()
                            where !x.IsDeleted && x.IsChecked && x.Category.ModelId == modelId
                            orderby x.Id descending
                            select new { x.Id, x.CategoryId, x.TemplateFileName, x.UpdateTime };

                return query.Take(top).ToArray().Select(x =>
                    new Core() { Id = x.Id, CategoryId = x.CategoryId, TemplateFileName = x.TemplateFileName,UpdateTime= x.UpdateTime }).ToArray();
            }
        }

        /// <summary>
        /// 读取被选择的Id范围的内容,返回Id,CategoryId,TemplateFileName
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="minId"></param>
        /// <param name="maxId"></param>
        /// <returns></returns>
        public IList<Core> ReadContent(byte modelId, int minId, int maxId)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Cores.AsNoTracking()
                            where !x.IsDeleted && x.IsChecked && x.Id >= minId && x.Id <= maxId
                            orderby x.Id descending
                            select new { Id = x.Id, CategoryId = x.CategoryId, x.TemplateFileName, x.UpdateTime };

                return query.ToArray().Select(x =>
                    new Core() { Id = x.Id, CategoryId = x.CategoryId, TemplateFileName = x.TemplateFileName, UpdateTime = x.UpdateTime }).ToArray();
            }
        }

        /// <summary>
        /// 读取被选择的Id范围的内容,返回Id,CategoryId,TemplateFileName
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="minDate"></param>
        /// <param name="maxDate"></param>
        /// <returns></returns>
        public IList<Core> ReadContent(byte modelId, DateTime minDate, DateTime maxDate)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Cores.AsNoTracking()
                            where !x.IsDeleted && x.IsChecked && x.UpdateTime >= minDate && x.UpdateTime <= maxDate
                            orderby x.Id descending
                            select new { x.Id, x.CategoryId, x.TemplateFileName, x.UpdateTime };

                return query.ToArray().Select(x =>
                    new Core() { Id = x.Id, CategoryId = x.CategoryId, TemplateFileName = x.TemplateFileName, UpdateTime = x.UpdateTime }).ToArray();
            }
        }




        #region 读取那些是启用生成静态而保持使用动态页的内容的栏目
        /// <summary>
        /// 读取那些是启用生成静态而保持使用动态页的内容的栏目
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public Dictionary<int, int> ContentUrlStyle(byte modelId)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Categoryes.AsNoTracking()
                        where x.LinkUrl == "" && x.ModelId == modelId
                        select new { x.Id, URLStyle = x.UrlStyle };

                return q.ToDictionary(x => x.Id, x => x.URLStyle);
            }
        }
        #endregion

        /// <summary>
        /// 取得专题栏目Id列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<Special> ReadSpecial(string strWhere)
        {
            using (Entities db = new Entities())
            {
                return db.Specials.SqlQuery("select Id,ModelId,SpecialName,SpecialName_EN,TemplateFileName from CMS_Special where LinkUrl=N'' and " + strWhere + " order by Id desc").ToArray();
            }
        }

        #region 栏目
        public IList<Category> ReadCategory(byte modelId)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Categoryes.AsNoTracking()
                        where x.ModelId == modelId && x.LinkUrl == ""
                        orderby x.Id descending
                        select x;
                return q.ToArray();
            }
        }
        public IList<Category> ReadCategory(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Categoryes.AsNoTracking()
                        where ids.Contains(x.Id) && x.LinkUrl == ""
                        orderby x.Id descending
                        select x;
                return q.ToArray();
            }
        }

        /// <summary>
        /// 栏目和它的子栏目的内容数量
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public int CountCategoryNo(int categoryId)
        {
            using (Entities db = new Entities())
            {
                string path = "|" + categoryId + "|";
                var query = db.Cores.AsNoTracking().Count(x => x.DeputyCategoryes.Any(y => y.Path.Contains(path)));
                return query;
            }
        }
        #endregion


    }
}